import { useEffect, useReducer } from "react";
import "./App.css";

function App() {

    // useReducer每次会把最新的state传递过来
    function reducer (state, action) {
        switch (action.type) {
            case 'add': 
                return state + action.num
            case 'minus': 
                return state - action.num
        }
    }

   const [count, dispatch] = useReducer(reducer, 0)

   useEffect(() => {
     setInterval(() => {
        dispatch({
            type: 'add',
            num: 1
        })
     }, 1000)
   }, [])

   return <>
    <div>{count}</div>
   </>
}

export default App;
